//
//  ByCellShopProduct.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/31.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellShopProduct.h"

// view
#import "ByViewVideoPlayer.h"       // 视频播放视图

@implementation ByCellShopProduct
{
    UIView      *_bgView;
    
    UIView      *_videoView;        // 视频播放视图
    
    UIView      *_descBgView;       // 描述背景视图，半透明
    UILabel     *_partLabel;        // 板块label（属于什么板块）
    UILabel     *_distanceLabel;    // 距离label
    
    UILabel     *_priceLabel;       // 现价
    UIView      *_tagsView;         // 标签
    UILabel     *_originPriceLabel; // 原价
    
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _bgView = [UIView new];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    _videoView = [ByViewVideoPlayer new];
    [_bgView addSubview:_videoView];
    
    _descBgView = [UIView new];
    _descBgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    [_bgView addSubview:_descBgView];
    
    _partLabel = [[UILabel alloc] init];
    _partLabel.text = @"西裤";
    _partLabel.font = FONT(14);
    _partLabel.textAlignment = NSTextAlignmentLeft;
    _partLabel.textColor = [UIColor whiteColor];
    [_descBgView addSubview:_partLabel];
    
    _distanceLabel = [[UILabel alloc] init];
    _distanceLabel.text = @"200m";
    _distanceLabel.font = FONT(12);
    _distanceLabel.textAlignment = NSTextAlignmentRight;
    _distanceLabel.textColor = [UIColor whiteColor];
    [_descBgView addSubview:_distanceLabel];
    
    _priceLabel = [[UILabel alloc] init];
    _priceLabel.text = @"￥299.88";
    _priceLabel.font = FONT(18);
    _priceLabel.textAlignment = NSTextAlignmentLeft;
    _priceLabel.textColor = BY_RED_COLOR;
    [_bgView addSubview:_priceLabel];
    
    _tagsView = [UIView new];
    [_bgView addSubview:_tagsView];
    
    _originPriceLabel = [[UILabel alloc] init];
    _originPriceLabel.text = @"￥399.88";
    _originPriceLabel.font = FONT(14);
    _originPriceLabel.textAlignment = NSTextAlignmentRight;
    _originPriceLabel.textColor = [UIColor lightGrayColor];
    [_bgView addSubview:_originPriceLabel];
    
    
    // 布局
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 10, 10, 10));
    }];
    [_videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.bottom.equalTo(@-40);
    }];
    
    [_descBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(_videoView);
        make.height.equalTo(@20);
    }];
    [_partLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@5);
        make.top.bottom.equalTo(@0);
        // 宽度自适应
    }];
    [_distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-5);
        make.top.bottom.equalTo(@0);
        // 宽度自适应
    }];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@5);
        make.top.equalTo(_videoView.mas_bottom);
        make.bottom.equalTo(@0);
        // 宽度自适应
    }];
    [_originPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-5);
        make.top.bottom.equalTo(_priceLabel);
        // 宽度自适应
    }];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self setTags];
    });
}

- (void)setTags {
    
    NSArray *tags = @[@"自营", @"折扣销售"];
    
    // 动态创建标签视图
    [_tagsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    _tagsView.left = _priceLabel.right + 5;
    _tagsView.height = 20;
    _tagsView.y = _priceLabel.y;
    CGFloat x = 0, y, w, h;
    for (int i = 0; i < tags.count; i ++) {
        y = 0;
        w = [tags[i] getSizeOfString:12].width + 10;
        h = _tagsView.height;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        label.text = tags[i];
        label.font = FONT(12);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = BY_RED_COLOR;
        label.layer.cornerRadius = 4;
        label.layer.masksToBounds = true;
        label.layer.borderColor = BY_RED_COLOR.CGColor;
        label.layer.borderWidth = 1;
        [_tagsView addSubview:label];
        
        x += w + 5;
    }
    _tagsView.width = _tagsView.subviews.lastObject.right;
}


@end
